<?php
namespace App\Http\Controllers\Company;

use App\Http\Controllers\Company\CompanyController;
use Illuminate\Http\Request;
use App\Models\CompanyJobs;
use App\Models\Area;
use Illuminate\Support\Facades\Lang;
use App\Models\Company;
use App\Models\SystemSetting;
use App\Models\Examination;
use Illuminate\Support\Facades\Response;

/**
 *
 * @author Joker
 *        
 */
class JobsController extends CompanyController
{
    
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 职位管理
     * @return [type] [description]
     */
    public function jobs(Request $req)
    {
        if(empty($this->userinfo->company_id))
            return redirect(url('/company/center/companyinfo-default'));
        
        $job_id = $req->input('id', 0);
        $blademode = $job_id ? 'edit' : 'add';
        $jobs = CompanyJobs::find($job_id);
        $exams = Examination::where('company_id',$this->userinfo->company_id)->get();
        $cat_id = 0;
        if($job_id){ //编辑
            $cat_id = $jobs->cat_id;
//            $select_area = Area::where('area_id', $jobs->area)->first();
//            $select_city = Area::where('area_id', $select_area->pid)->first();
            $select_city = Area::where('area_id', $jobs->area)->first();
            $select_province = Area::where('area_id', $select_city->pid)->first();
    
            $subarea= Area::where('pid','=',$select_province->area_id)->orderBy('sort','ASC')->get();
            $thrarea= Area::where('pid','=',$select_city->area_id)->orderBy('sort','ASC')->get();
        }

        $view = view('company.jobsmanage')
            ->with('nav', $this->nav)
            ->with('type', 'all')
            ->with('options', $this->getJobsTypeOptions($cat_id))
            ->with('company_id', $this->uid)
            ->with('gznx', $this->gznx)
            ->with('salary', $this->salary)
            ->with('degree', $this->degree)
            ->with('area', $this->area)
            ->with('blademode', $blademode)
            ->with('examinations',$exams)
            ->with('job', $jobs);
        if($job_id) { //编辑
            $view->with('select_province', $select_province)
                ->with('select_city', $select_city)

                ->with('subarea', $subarea)
                ->with('thrarea', $thrarea)->with('examinations',$exams);
        }
        return $view;
    
    }
    
    /**
     * 职位列表
     * @return [type] [description]
     */
    public function joblist($type)
    {
        if(empty($this->userinfo->company_id))
            return redirect(url('/company/center/companyinfo-default'));
        
        $where = [];
        switch ($type) {
            case 'all':
                # code...
                break;
            case 'valid':
                $where = ['status' => 1];
                break;
            case 'offline':
                $where = ['status' => 3];
                break;
    
            default:
                # code...
                break;
        }
        $where['company_id'] = $this->userinfo->company_id;
        $jobs = CompanyJobs::where($where)->paginate(15);
        return view('company.alljobs')
        ->with('nav', $this->nav)
        ->with('type', $type)
        ->with('jobs', $jobs);
    }
    
    /**
     * 保存职位
     * @return [type] [description]
     */
    public function savejobs(Request $req)
    {
        if(empty($this->userinfo->company_id) || $this->userinfo->company_id == 0)
        {
            return redirect('/company/center/jobs/save')->withErrors(['no_company'=>Lang::get('company.no_company')]);
        }
        $blademode = $req->input('blademode');
        $post = array_except($req->all(), ['_token','province', 'city', 'blademode', 'job_id']);
        $company = Company::find($this->userinfo->company_id);
        if(empty($company))
            return redirect('/company/center/jobs/save')->withErrors(['no_company'=>Lang::get('company.no_company')]);
        $sysword = SystemSetting::where('varname','=','sensitive_words')->first();
        $wordstr = unserialize($sysword['value']);
        $post['title']      = filter_words($post['title'], $wordstr);
        $post['intro']      = filter_words($post['intro'], $wordstr);
        $post['description']= filter_words($post['description'], $wordstr);

        if(empty($post['cat_id'] ) || $post['cat_id']  == 0)
        {
            return redirect()->back()->withErrors(['no_cat_id'=>Lang::get('company.no_cat_id')]);
        }
        if(empty($post['title']) || empty($post['intro']) || empty($post['description']) || empty($post['recruitment']))
        {
            return redirect()->back()->withErrors(['no_msg'=>Lang::get('company.no_msg')]);
        }

        if($blademode == 'add')
        {
            $post['status'] = 0;
            $post['user_id'] = $this->uid;
            $post['company_id'] = $this->userinfo->company_id;
            $jobs = CompanyJobs::firstOrCreate($post);
            $company->incrementAdd('jobs_count',1);
        }
        else
        {
            $jobs = CompanyJobs::find($req->input('job_id', 0));
            foreach ($post as $key => $value) {
                $jobs->$key = $value;
            }
            $jobs->save();
        }
        if($jobs->jobs_id)
            return redirect('/company/center/jobs-all');
        else
            return redirect('/company/center/jobs/save')->withInput($post);
    }

    /**
     * 改变职位状态
     * @param Request $req
     */
    public function changestatus(Request $req)
    {
        $jobs_id = $req->input('jobs_id', 0);
        $status = $req->input('status', 0);
        $jobs = CompanyJobs::find($jobs_id);
        $jobs->status = $status;
        if($jobs->save())
            return  $this->formatJson('success','成功！');
        else
            return  $this->formatJson('success','失败！');
    }
}